import {Component} from '@angular/core';
import {IonicPage, NavController, NavParams} from 'ionic-angular';
import {ROUTER} from "../../service/router.service";
import {CheckStandModel} from "../../model/checkstand.model";
import {LocalStorage} from "../../utils/localstorage.utils";
import {METER} from "../../api/meter.api";
import {TopTabModel} from "../../model/topTab.model";

/**
 * Generated class for the RechargePage page.
 *
 * See http://ionicframework.com/docs/components/#navigation for more info
 * on Ionic pages and navigation.
 */
@IonicPage()
@Component({
  selector: 'page-life-recharge',
  templateUrl: 'life-recharge.html',
})
export class LifeRechargePage {

  data = {
    contractId: undefined,
    money: 50,
    type: 2
  };

  moneyItems = [
    {name: '50元', value: 50},
    {name: '100元', value: 100},
    {name: '300元', value: 300},
    {name: '500元', value: 500},
    {name: '1000元', value: 1000},
    {name: '3000元', value: 3000}
  ];
  typeItems:Array<TopTabModel> = [];

  constructor(public METER:METER, public navCtrl:NavController, public ls:LocalStorage, public navParams:NavParams, public ROUTER:ROUTER) {
    this.data.contractId = this.ls.get('lcContractId')
    this.typeItems.push(new TopTabModel({id: 2, name: '充水费', isSelect: true, func: this.tabChange}));
    this.typeItems.push(new TopTabModel({id: 3, name: '充电费', func: this.tabChange}));
  }

  tabChange = v=> {
    for (let item of this.typeItems) {
      item.isSelect = false
    }
    v.isSelect = true;
    this.data.type = v.id;
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad RechargePage');
  }

  moneyPick(v) {
    this.data.money = v.value;
  }

  typePick(v) {
    this.data.type = v.value;
  }

  goCheckstand() {
    this.METER.recharge(this.data, v=> {
      console.log(v)
      let checkstand = new CheckStandModel(v.uuid, v.content, v.money, v.id, ROUTER.LIFEPAYMENT);
      this.ls.setObject('checkstand', checkstand);
      this.ROUTER.go(ROUTER.CHECK);
    })
  }
}
